package tree;

import java.util.ArrayList;
import java.util.List;

public class AfterOrder {
    public List<Integer> postorderTraversal(TreeNode root) {
        ArrayList<Integer> list = new ArrayList<>();
        if (root !=null){
            postorder(root,list);
        }
        return list;

    }

    private void postorder(TreeNode root, ArrayList<Integer> list) {
        if (root==null){
            return;
        }
        postorder(root.left,list);
        postorder(root.right,list);
        list.add(root.val);
    }
    public static void main(String[] args) {
        // 创建树
        TreeNode node7 = new TreeNode(7, null, null);
        TreeNode node6 = new TreeNode(6, null, null);
        TreeNode node8 = new TreeNode(8, null, null);
        TreeNode node9 = new TreeNode(9, node6, node7);
        TreeNode root = new TreeNode(3, node9, node8);

        // 调用打印方法
        AfterOrder afterOrder = new AfterOrder();
        List<Integer> result = afterOrder.postorderTraversal(root);

        // 输出结果
        System.out.println(result);
    }

}
